package cn.kxx.controller;

import cn.kxx.config.QiniuUtils;
import cn.kxx.constant.MessageConstant;
import cn.kxx.constant.RedisConstant;
import cn.kxx.entity.PageResult;
import cn.kxx.entity.QueryPageBean;
import cn.kxx.entity.Result;
import cn.kxx.exception.BusinessException;
import cn.kxx.pojo.Setmeal;
import cn.kxx.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Api("套餐管理")
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    RedisTemplate redisTemplate;
    @Reference
    private SetmealService setmealService;
    @ApiOperation("上传文件到七牛云")
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
            //1.对图片进行重新命名
            //1.1.获取原文件名
            String originalFilename = imgFile.getOriginalFilename();
            int i = originalFilename.lastIndexOf(".");
            //1.2.获取后缀
            String suffix = originalFilename.substring(i);
            //1.3.重新命名
            String newname =UUID.randomUUID().toString()+suffix;
            //2.上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),newname);
            redisTemplate.opsForSet().add(RedisConstant.UPLOAD_SETMEAL_QNY_KEY,newname);
            return new Result(true, MessageConstant.UPLOAD_SUCCESS,newname);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("文件上传失败");
        }
    }
    @ApiOperation("添加套餐")
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        setmealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @ApiOperation("分页查询，可根据套餐助记码、名称、编码进行查询")
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }


    @ApiOperation("根据id获取套餐基本信息")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id")Integer id){
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    @ApiOperation("查询套餐对应的检查组id集合")
    @GetMapping("/findCheckGroupIdsBySetmealId/{id}")
    public Result findCheckGroupIdsBySetmealId(@PathVariable("id")Integer id){
        List<Integer> groupIds = setmealService.findCheckGroupIdsBySetmealId(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,groupIds);
    }

    @ApiOperation("编辑套餐")
    @PutMapping("/edit")
    public Result edit(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
        setmealService.edit(checkgroupIds,setmeal);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }


    @ApiModelProperty("删除检查组")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){

        setmealService.delete(id);
        Result result = new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        return result;
    }

}